﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class ChiettinhKhachSan : AdminPageBase
    {

        protected List<CalculatorInfo> TicketHotel = new List<CalculatorInfo>();
        protected List<CalculatorInfo> TicketShip = new List<CalculatorInfo>();
        protected decimal TotalTicketHotelPrices = 0;
        protected decimal TotalTicketHotelPricesAdults = 0;
        protected decimal TotalTicketHotelPricesChild = 0;
        protected decimal TotalTicketHotelPricesBaby = 0;

        protected decimal TotalTicketShipPrices = 0;
        protected decimal TotalTicketShipPricesAdults = 0;
        protected decimal TotalTicketShipPricesChild = 0;
        protected decimal TotalTicketShipPricesBaby = 0;

        private List<CalculatorInfo> _calculatorList = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorController = new CalculatorController();
        protected TourInfo tourInfo = new TourInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _calculatorList =
                    _calculatorController.ListCalculator()
                        .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"])).ToList();
                tourInfo = new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
            }
            LoadHotelType();
            LoadNational();
            LoadHotelTypeShip();
            LoadNationalShip();
            LoadScheduleShip();
            if (_calculatorList.Count == 0) return;
            RenderForm();
        }

        public void RenderForm()
        {

            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
               //khach san
                TicketHotel = _calculatorList.Where(p => p.ItemType == 5).ToList();
                ////TotalTicketHotelPricesAdults = TicketHotel.Sum(p =>p.TotalPriceHotelAdults);
                ////TotalTicketHotelPricesChild = TicketHotel.Sum(p => p.TotalPriceHotelChild);
                ////TotalTicketHotelPrices = TotalTicketHotelPricesAdults + TotalTicketHotelPricesChild;
                TotalTicketHotelPricesAdults = TicketHotel.Sum(p => p.TotalPriceHotelAdults) / tourInfo.QuantityAdult;
                TotalTicketHotelPricesChild = TicketHotel.Sum(p => p.TotalPriceHotelChild) / tourInfo.QuantityChild;
                TotalTicketHotelPricesBaby = TicketHotel.Sum(p => p.TotalPriceHotelBaby) / tourInfo.QuantityBaby;
                TotalTicketHotelPrices = TicketHotel.Sum(p => p.TotalPriceHotelAdults) +
                                         TicketHotel.Sum(p => p.TotalPriceHotelChild) +
                                         TicketHotel.Sum(p => p.TotalPriceHotelBaby);
                //tauthuy
                TicketShip = _calculatorList.Where(p => p.ItemType == 10).ToList();
                ////TotalTicketHotelPricesAdults = TicketHotel.Sum(p =>p.TotalPriceHotelAdults);
                ////TotalTicketHotelPricesChild = TicketHotel.Sum(p => p.TotalPriceHotelChild);
                ////TotalTicketHotelPrices = TotalTicketHotelPricesAdults + TotalTicketHotelPricesChild;
                TotalTicketShipPricesAdults = TicketShip.Sum(p => p.TotalPriceHotelAdults) / tourInfo.QuantityAdult;
                TotalTicketShipPricesChild = TicketShip.Sum(p => p.TotalPriceHotelChild) / tourInfo.QuantityChild;
                TotalTicketShipPricesBaby = TicketShip.Sum(p => p.TotalPriceHotelBaby) / tourInfo.QuantityBaby;
                TotalTicketShipPrices = TicketShip.Sum(p => p.TotalPriceHotelAdults) +
                                         TicketShip.Sum(p => p.TotalPriceHotelChild) +
                                         TicketShip.Sum(p => p.TotalPriceHotelBaby);
            }
        }

        public void LoadHotelType()
        {
            ddlHotelType.DataSource = new HotelTypeController().ListHotelType().OrderBy(p=>p.HotelTypeName);
            ddlHotelType.DataValueField = "HotelTypeId";
            ddlHotelType.DataTextField = "HotelTypeName";
            ddlHotelType.DataBind();

            ddlHotelType.Items.Insert(0,new ListItem("--Chọn loại--","-1"));
        }
        public void LoadNationalShip()
        {
            ddlNationalShip.DataSource = new NationalController().ListNational().OrderBy(p => p.NationalName);
            ddlNationalShip.DataValueField = "NationalId";
            ddlNationalShip.DataTextField = "NationalName";
            ddlNationalShip.DataBind(); 
            ddlNationalShip.Items.Insert(0, new ListItem("--Chọn quốc gia--", "-1"));
        }
        public void LoadHotelTypeShip()
        {
            ddlHotelTypeShip.DataSource = new HotelTypeController().ListHotelType().OrderBy(p => p.HotelTypeName);
            ddlHotelTypeShip.DataValueField = "HotelTypeId";
            ddlHotelTypeShip.DataTextField = "HotelTypeName";
            ddlHotelTypeShip.DataBind();
            ddlHotelTypeShip.Items.Insert(0, new ListItem("--Chọn loại--", "-1"));
        }
        private void LoadScheduleShip()
        {
            ddlScheduleShip.DataSource =
                new ScheduleController().ListSchedule()
                    .Where(p => p.Status == (int)Globals.DocumentStatus.Use && p.ScheduleType == 3)
                    .OrderBy(p => p.ScheduleName);

            ddlScheduleShip.DataTextField = "ScheduleName";
            ddlScheduleShip.DataValueField = "ScheduleId";
            ddlScheduleShip.DataBind();
            ddlScheduleShip.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }
        public void LoadNational()
        {
            ddlNationalHotel.DataSource = new NationalController().ListNational().OrderBy(p => p.NationalName);
            ddlNationalHotel.DataValueField = "NationalId";
            ddlNationalHotel.DataTextField = "NationalName";
            ddlNationalHotel.DataBind();
            ddlNationalHotel.Items.Insert(0, new ListItem("--Chọn quốc gia--", "-1"));
        }

    }
}